/**
 * Copyright(c)2012 Beijing PeaceMap Co.,Ltd.
 * All right reserved. 
 */
package com.pmc.dwa.common.controller;

import java.util.Enumeration;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

/**
 * @description 直接访问WEB-INF中的pages页面，如果带参数在页面上使用EL表达式取出，如：${参数名}或者${param.参数名}
 * @author aokunsang
 * @date 2013-1-6
 */
@Controller
@RequestMapping("/jsp")
public class JspViewRedirectController {

	/* 跳转到带有2层目录的页面 */
	@RequestMapping(value="/{forder1}/{forder2}/{jspview}",method=RequestMethod.GET)
	public String jsp2Redirect(
			@PathVariable String forder1,
			@PathVariable String forder2,
			@PathVariable String jspview,
			HttpServletRequest request,
			ModelMap param){
		buildParamaters(request,param);
		return forder1+"/"+forder2+"/"+jspview;
	}
	
	/* 跳转到带有1层目录的页面 */
	@RequestMapping(value="/{forder}/{jspview}",method=RequestMethod.GET)
	public String jsp1Redirect(
			@PathVariable String forder,
			@PathVariable String jspview,
			HttpServletRequest request,
			ModelMap param){
		buildParamaters(request,param);
		return forder+"/"+jspview;
	}
	
	/* 直接跳转到页面*/
	@RequestMapping(value="/{jspview}",method=RequestMethod.GET)
	public String jspSingleRedirect(
			@PathVariable String jspview,
			HttpServletRequest request,
			ModelMap param){
		buildParamaters(request,param);
		return jspview;
	}
	
	/* 封装将要传递的参数列表*/
	private void buildParamaters(HttpServletRequest request,ModelMap param){
		Enumeration<String> fieldNames = request.getParameterNames();
		while(fieldNames.hasMoreElements()){
			String fieldName = fieldNames.nextElement();
			String fieldValue = request.getParameter(fieldName);
			param.put(fieldName, fieldValue);
		}
	}
}
